//
//  LXTopSearchView.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/31.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXTopSearchView.h"

@implementation LXTopSearchView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];

    if (self) {
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width,  self.height)];
        topView.backgroundColor = UIColor.whiteColor;
        [self addSubview:topView];
        self.topView = topView;
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 15, topView.width-70, 40)];
        textField.backgroundColor = ColorWithF5F5F5;
        textField.placeholder = @"请输入";
        textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 0)];
        textField.leftViewMode = UITextFieldViewModeAlways;
        textField.clearButtonMode = UITextFieldViewModeAlways;
        [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
        [topView addSubview:textField];
        textField.layer.cornerRadius = 3;
        textField.layer.masksToBounds = YES;
        textField.font = [UIFont systemFontOfSize:14];
        self.textField = textField;
        
        UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(MaxX(textField), 0, self.width-MaxX(textField), topView.height)];
        [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [searchBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [searchBtn addTarget:self action:@selector(clickSearch) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:searchBtn];
        self.searchBtn = searchBtn;
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIcon"]];
        imageView.X = 10;
        imageView.Y = (textField.height-imageView.height)*0.5;
        [textField addSubview:imageView];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(MaxX(imageView)+8, 10, 0.5, textField.height-20)];
        line.backgroundColor = [UIColor colorWithHexString:@"#DDDDDD"];
        [textField addSubview:line];
        
        
    }
    return self;
    
}

-(void)clickSearch{
    if (self.clickBtnBlock) {
        self.clickBtnBlock(self.textField.text);
    }
}
- (void)textChanged:(UITextField *)textField{
    
    if (self.changeTextFieldBlock) {
        self.changeTextFieldBlock(self.textField.text);
    }
    
    
}
@end
